## Replication Transparency and Bureaucracy

library("dplyr")
library("lmtest")
library("sandwich")
library("cem")
library("sjPlot")
library("brazilmaps")
library("sf")
library("ggpubr")
library("marginaleffects")



load("TranspBureaucracy.Rda")




## CEM

# Adoption

tre<-tbur[c("ADESAO","Reeleito3" ,"Ideologia2", "percent_voto", "LogPibCapita","ClassePop","PropComissionado2020")]


tre<-na.omit(tre)

mat <- cem(treatment = "ADESAO", data = tre, drop = "PropComissionado2020",keep.all=TRUE)

est <- att(mat, PropComissionado2020 ~ ADESAO, data = tre)


est

#
tre<-tbur[c("ADESAO","Reeleito3" ,"Ideologia2", "percent_voto", "LogPibCapita","ClassePop","PropSemVinculo2020")]


tre<-na.omit(tre)

mat <- cem(treatment = "ADESAO", data = tre, drop = "PropSemVinculo2020",keep.all=TRUE)

est <- att(mat, PropSemVinculo2020 ~ ADESAO, data = tre)


est


#
tre<-tbur[c("ADESAO","Reeleito3" ,"Ideologia2", "percent_voto", "LogPibCapita","ClassePop","Patronagem2020")]


tre<-na.omit(tre)

mat <- cem(treatment = "ADESAO", data = tre, drop = "Patronagem2020",keep.all=TRUE)

est <- att(mat, Patronagem2020 ~ ADESAO, data = tre)


est



## Diff-in-Diff

tre<-tbur[c("Adoption","Reelected" ,"Ideology", "VoteShare", "LnGDPCapita","PopClass","VarComissionado")]


tre<-na.omit(tre)

mat <- cem(treatment = "Adoption", data = tre, drop = "VarComissionado",keep.all=TRUE)

est <- att(mat, VarComissionado ~ Adoption, data = tre)
est


#

tre<-tbur[c("Adoption","Reelected" ,"Ideology", "VoteShare", "LnGDPCapita","PopClass","VarSemVinculo")]


tre<-na.omit(tre)

mat <- cem(treatment = "Adoption", data = tre, drop = "VarSemVinculo",keep.all=TRUE)

est <- att(mat, VarSemVinculo ~ Adoption, data = tre)
est


#

tre<-tbur[c("Adoption","Reelected" ,"Ideology", "VoteShare", "LnGDPCapita","PopClass","VarPatronagem")]


tre<-na.omit(tre)

mat <- cem(treatment = "Adoption", data = tre, drop = "VarPatronagem",keep.all=TRUE)

est <- att(mat, VarSemVinculo ~ Adoption, data = tre)
est



### Maps

### Mapas


tre<-tbur[c("COD_MUNIC_IBGE", "PropComissionado2020", "PropSemVinculo2020", "Patronagem2020")]

names(tre)[names(tre) == 'COD_MUNIC_IBGE'] <- 'City'



muni_map <- get_brmap("City") %>% 
  left_join(tre, c("City" = "City"))


map1<-muni_map %>% 
  ggplot() +
  geom_sf(aes(fill = Patronagem2020), 
          colour = "transparent", size = 0.1) +
  scale_fill_distiller(palette = "Reds",direction=1, guide = "colorbar",na.value="white",
                       name = "Patronage")+ 
  theme(panel.grid = element_line(colour = "transparent"),
        panel.background = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())


map2<-muni_map %>% 
  ggplot() +
  geom_sf(aes(fill = PropComissionado2020), 
          colour = "transparent", size = 0.1) +
  scale_fill_distiller(palette = "Blues",direction=1, guide = "colorbar",na.value="white",
                       name = "Control")+ 
  theme(panel.grid = element_line(colour = "transparent"),
        panel.background = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())

map3<-muni_map %>% 
  ggplot() +
  geom_sf(aes(fill = PropSemVinculo2020), 
          colour = "transparent", size = 0.1) +
  scale_fill_distiller(palette = "Greens",direction=1, guide = "colorbar",na.value="white",
                       name = "Reward")+ 
  theme(panel.grid = element_line(colour = "transparent"),
        panel.background = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())


ggarrange(map1,                                         
          ggarrange(map2, map3, ncol = 2), 
          nrow = 2)

###

tre<-tbur[c("COD_MUNIC_IBGE","Adesao2","Formal3", "PESSOAL","PassiveB")]

names(tre)[names(tre) == 'COD_MUNIC_IBGE'] <- 'City'

tre$PESSOAL<-as.factor(tre$PESSOAL)
tre$PassiveB<-as.factor(tre$PassiveB)


muni_map <- get_brmap("City") %>% 
  left_join(tre, c("City" = "City"))


map1<-muni_map %>% 
  ggplot() +
  geom_sf(aes(fill = Adesao2), 
          colour = "transparent", size = 0.1) +
  scale_fill_manual(labels = c("0" = "N",
                               "1" = "Y"), values=c("0" = "red", "1" = "blue"), name = "Adoption")+ 
  theme(panel.grid = element_line(colour = "transparent"),
        panel.background = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())


map2<-muni_map %>% 
  ggplot() +
  geom_sf(aes(fill = Formal3), 
          colour = "transparent", size = 0.1) +
  scale_fill_distiller(palette = "Blues",direction=1, guide = "colorbar",na.value="white",
                       name = "Formal Strength")+ 
  theme(panel.grid = element_line(colour = "transparent"),
        panel.background = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())


map3<-muni_map %>% 
  ggplot() +
  geom_sf(aes(fill = PESSOAL), 
          colour = "transparent", size = 0.1) +
  scale_fill_manual(labels = c("0" = "N",
                               "1" = "Y"), values=c("0" = "rosybrown", "1" = "purple"), name = "Active Transparency")+
  theme(panel.grid = element_line(colour = "transparent"),
        panel.background = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())

map4<-muni_map %>% 
  ggplot() +
  geom_sf(aes(fill = PassiveB), 
          colour = "transparent", size = 0.1) +
  scale_fill_manual(labels = c("0" = "N",
                               "1" = "Y"), values=c("0" = "lightgreen", "1" = "darkgreen"), name = "Passive Transparency")+
  theme(panel.grid = element_line(colour = "transparent"),
        panel.background = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())

ggarrange(map1, map2, map3, map4)



####


municipios <- get_brmap(geo = "City", geo.filter =NULL,
                        class = "SpatialPolygonsDataFrame")

map1<-plot_brmap(municipios, data_to_join = data.frame(tre), join_by = "City",
                 var = "Patronagem2020", theme = theme_map()) +
  scale_fill_distiller(palette = "Reds",direction=1, guide = "colorbar",na.value="white",
                       name = "Patronage")


map2<-plot_brmap(municipios, data_to_join = data.frame(tre), join_by = "City",
                 var = "PropComissionado2020", theme = theme_map()) +
  scale_fill_continuous(low = "lightblue", high = "darkblue", guide="colorbar",na.value="white",
                        name = "Political Control")

map3<-plot_brmap(municipios, data_to_join = data.frame(tre), join_by = "City",
                 var = "PropSemVinculo2020", theme = theme_map()) +
  scale_fill_continuous(low = "lightgreen", high = "darkgreen", guide="colorbar",na.value="white",
                        name = "Reward")



ggarrange(map1, map2, map3, labels = c('Patronage',"Political Control", "Reward"), ncol=3)


ggarrange(map1,                                         
          ggarrange(map2, map3, ncol = 2, labels = c("Political Control", "Reward")), 
          nrow = 2, 
          labels = "Patronage"                           
) 

#

tre<-tbur[c("COD_MUNIC_IBGE","Adesao2","Formal3", "PESSOAL","PassiveB")]

names(tre)[names(tre) == 'COD_MUNIC_IBGE'] <- 'City'

tre$PESSOAL<-as.factor(tre$PESSOAL)
tre$PassiveB<-as.factor(tre$PassiveB)



municipios <- get_brmap(geo = "City", geo.filter =NULL,
                        class = "SpatialPolygonsDataFrame")



map1<-plot_brmap(municipios, data_to_join = data.frame(tre), join_by = "City",
                 var = "Adesao2", theme = theme_map())+
  scale_fill_manual(labels = c("0" = "N",
                               "1" = "Y"), values=c("0" = "red", "1" = "blue"), name = "Adoption")


map2<-plot_brmap(municipios, data_to_join = data.frame(tre), join_by = "City",
                 var = "Formal3", theme = theme_map()) +
  scale_fill_continuous(low = "lightblue", high = "darkblue", guide="colorbar",na.value="white",
                        name = "Formal Strength")

map3<-plot_brmap(municipios, data_to_join = data.frame(tre), join_by = "City",
                 var = "PESSOAL", theme = theme_map())+
  scale_fill_manual(labels = c("0" = "N",
                               "1" = "Y"), values=c("0" = "rosybrown", "1" = "purple"), name = "Active Transparency")


map4<-plot_brmap(municipios, data_to_join = data.frame(tre), join_by = "City",
                 var = "PassiveB", theme = theme_map())+
  scale_fill_manual(labels = c("0" = "N",
                               "1" = "Y"), values=c("0" = "lightgreen", "1" = "darkgreen"), name = "Passive Transparency")




ggarrange(map1, map2, map3, map4, labels = c('Adoption',"Formal Strength", "Active Transparency", "Passive Transparency"))

###
